<?php

/**
 * 角色模型
 *
 * @author JiangJian <silverd@sohu.com>
 * @copyright 2011-2012 xiangle.com
 * $Id: Role.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @version    2012-02-16  ::  JiangJian  ::  Create File
 */
class Model_System_Role extends Model_AbstractDao
{

    protected $_dbName     = 'BingoMobi';
    protected $_tableName  = 'bg_sys_role';
    protected $_primaryKey = 'role_id';
    protected $_nameField  = 'role_name';

    /**
     * 添加一条
     *
     * @param array $data
     * @return bool
     */
    public function add($data)
    {
        $data = Helper_String::deepFilterDatas($data, array('trim', 'strip_tags'));

        // 检测合法性
        if (!$data['role_name']) {
            return '角色名不能为空';
        }

        $setArr = array(
            'role_name'  => $data['role_name'],
            'privileges' => $data['privileges'] ? implode(',', $data['privileges']) : '',
        );

        $insertId = $this->_db()->insert('bg_sys_role', $setArr);
        if ($insertId < 1) {
            return '添加角色入库失败';
        }

        return true;
    }

    /**
     * 更新一条
     *
     * @param int $roleId
     * @param array $data
     * @return bool
     */
    public function update1($roleId, $data)
    {
        $roleId = intval($roleId);
        if ($roleId < 1) {
            return '角色id非法';
        }

        $data = Helper_String::deepFilterDatas($data, array('trim', 'strip_tags'));

        // 检测合法性
        if (!$data['role_name']) {
            return '角色名不能为空';
        }

        $setArr = array(
            'role_name'  => $data['role_name'],
            'privileges' => $data['privileges'] ? implode(',', $data['privileges']) : '',
        );

        if (!$this->_db()->update('bg_sys_role', $setArr, array('role_id' => $roleId))) {
            return '更新角色入库失败';
        }

        return true;
    }

}